package cn.iocoder.an.module.business.job;

import cn.iocoder.an.framework.quartz.core.handler.JobHandler;
import cn.iocoder.an.framework.tenant.core.job.TenantJob;
import cn.iocoder.an.module.business.controller.admin.storemanage.vo.StoreManagePageReqVO;
import cn.iocoder.an.module.business.dal.dataobject.filedaily.FileDailyDO;
import cn.iocoder.an.module.business.dal.dataobject.reportview.ReportViewDO;
import cn.iocoder.an.module.business.dal.dataobject.reportviewnew.ReportConfigDO;
import cn.iocoder.an.module.business.dal.dataobject.storeinit.StoreInitDO;
import cn.iocoder.an.module.business.dal.dataobject.storeinitbackup.StoreInitBackupDO;
import cn.iocoder.an.module.business.dal.mysql.filedaily.FileDailyMapper;
import cn.iocoder.an.module.business.dal.mysql.reportconfig.ReportConfigMapper;
import cn.iocoder.an.module.business.dal.mysql.reportview.ReportViewMapper;
import cn.iocoder.an.module.business.dal.mysql.storeinit.StoreInitMapper;
import cn.iocoder.an.module.business.dal.mysql.storeinitbackup.StoreInitBackupMapper;
import cn.iocoder.an.module.business.dal.mysql.storemanage.StoreManageMapper;
import cn.iocoder.an.module.business.service.filedaily.FileDailyService;
import cn.iocoder.an.module.business.utils.WeComMessage;
import cn.iocoder.an.module.business.utils.WeComRobotUtil;
import cn.iocoder.an.module.business.utils.strategy.ActionDispatcher;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component

public class ReportMessageSend implements JobHandler {

    @Resource
    private FileDailyService fileDailyService;

    @Resource
    private WeComRobotUtil weComRobotUtil;


    @Value("${wecom.robot.default-webhook}") // ✅ 正确使用 Spring 的 @Value
    private String defaultWebhook;

    @Resource
    private ReportViewMapper reportViewMapper;

    @Resource
    private ReportConfigMapper reportConfigMapper;

    @Autowired
    private ActionDispatcher dispatcher;


    @Override
    public String execute(String param) throws Exception {

        List<ReportViewDO> reportViewDOS = reportViewMapper.selectList();
        for (ReportViewDO viewDO : reportViewDOS) {
            Long reportId = viewDO.getId();
            List<ReportConfigDO> configDOList = reportConfigMapper.selectListByReportId(reportId);
            for (ReportConfigDO configDO : configDOList) {
                String name = configDO.getName();
                //策略模式 目标字符串
                String targetFunction = configDO.getTargetFunction();
                //目标值
                BigDecimal targetValue = configDO.getTargetValue();
                if (StringUtils.isEmpty(targetFunction) || targetValue == null) {
                    continue;
                }


                Map<String, Object> params = new HashMap<>();
                params.put("method",targetFunction);
                params.put("targetValue",targetValue.longValue());
                params.put("name",name);
                    try {
                        Object result = dispatcher.execute(targetFunction, params);
//                        return ResponseEntity.ok(result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
//                        return ResponseEntity.badRequest().body(e.getMessage());
                    }
            }


        }

        return "success";
    }

}
